package com.SoilMM.controller;


import com.SoilMM.entity.GtimeMonitEntity;
import com.SoilMM.service.GtimeMonitService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gramorgan
 * @since 2021-06-23
 */
@RestController
@RequestMapping("/RtimeData")
public class GtimeMonitController {
    @Autowired
    private GtimeMonitService gtimeMonitService;
    @RequestMapping("/show")
    public Map<String, Object> showRtimeData(int cha, int pageSize, int currentPage){
        System.out.println("我是控制器方法");
        Map<String, Object> resultMap = new HashMap<>();
        List<Object> list = new ArrayList<>();
        GtimeMonitEntity gtimeMonitEntity = new GtimeMonitEntity();
        // 当前页，当前页展示的条数
        /*cha = cha-24;
        for (int i = 0; i <cha ; i++) {
            int j = 0;
            j = i%24;
            gtimeMonitEntity = gtimeMonitService.list().get(j);
            System.out.println(gtimeMonitEntity);
            gtimeMonitService.save(gtimeMonitEntity);
        }*/
        if(currentPage==1+cha/pageSize){
            pageSize = cha%pageSize;
            System.out.println(pageSize);
        }
            currentPage = 1+currentPage%2;
        Page page = new Page(currentPage,pageSize);
        IPage resultPage = gtimeMonitService.page(page);
        resultPage.setTotal(cha);

//        System.out.println(resultPage);
//        resultPage.condition();
//        cha = cha/12;
//        for (int i = 0; i <cha ; i++) {
//            IPage resultPage = gtimeMonitService.page(page);
//            list.add(resultPage);
//        }

        resultMap.put("code",10000);
        resultMap.put("message","查询成功");
        resultMap.put("data",resultPage);
        return resultMap;
//        GtimeMonitEntity gtimeMonitEntity = new GtimeMonitEntity();
//        gtimeMonitEntity = gtimeMonitService.showRtimeData(time);
//        if(gtimeMonitEntity != null){
//            resultMap.put("code",10000);
//            resultMap.put("message","查询成功");
//            resultMap.put("data",gtimeMonitEntity);
//        } else {
//            resultMap.put("code",10001);
//            resultMap.put("message","查询不存在,请检查输入");
//        }
//        return resultMap;

    }

    @RequestMapping("/list")
    public Map<String, Object> showRtimeData() {
        System.out.println("我是控制器方法");
        Map<String, Object> resultMap = new HashMap<>();
        List<GtimeMonitEntity> list = gtimeMonitService.list();
        resultMap.put("code",111);
        resultMap.put("message","查询成功");
        resultMap.put("data", list);
        return resultMap;

    }
    @RequestMapping("/qshow")
    public Map<String,Object> qshow(int currentPage, int pageSize){
        Map<String,Object> map=new HashMap<>();

        Page page=new Page(currentPage,pageSize);

        IPage result=gtimeMonitService.page(page);

        if (result.getTotal()>0)
        {
            map.put("data",result);
            map.put("message","查询成功");
        }
        else {
            map.put("message","暂无数据");
        }
        return map;
    }


}

